package com.om39a.plm.parser;

import java.util.List;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

/**
 * @author om39a
 * 
 */
public class ParserForumUrl extends PLMParser {

	static Logger logger = Logger.getLogger(ParserForumUrl.class);

	public void parse(String mainForumContent, int minReplies,
			List<String> forumTopicURLs) {

		logger.info("Entring into parse - mainForumContent " + minReplies
				+ " - " + "forumTopicURLs");

		Document doc = Jsoup.parse(mainForumContent);
		// Get the thread url's and number of replies
		Elements threadTopics = doc.getElementsByAttributeValue("class",
				"topic-name");
		for (Element element : threadTopics) {
			List<TextNode> textNodes = element.textNodes();
			if (textNodes.size() >= 1) {
				// Checking if the number of replies are greater than 10
				if (Integer.valueOf(textNodes.get(0).text()
						.replaceAll("\\D", "").trim()) >= minReplies) {
					forumTopicURLs.add(element.select("a[href]").attr("href")
							.trim());
				}
			}
		}
		logger.info("Exiting from parse");
	}
}
